const Task = require('../models/task')
const Journal = require('../models/journal')
const User = require('../models/user')
const Category = require('../models/category')
const Notic = require('../models/notice')

const response = require('../utils/response')
const loop = require('../utils/loop')
const fs = require('fs')
let path = require('path')
const task = require('../models/task')
const {
    body
} = require('express-validator')

const {huanTime} = require('../utils/tool')

// 根据时间获取消息列表
exports.getList = async (req,res,next) => {
    try {

        //校验是否时间 完整格式  2021/11/28 00:00:00
        // function check(time) {
        //     var r = time.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/);
        //     if (r == null) return false;
        //     var d = new Date(r[1], r[3] - 1, r[4], r[5], r[6], r[7]);
        //     return (d.getFullYear() == r[1] && (d.getMonth() + 1) == r[3] && d.getDate() == r[4] && d.getHours() == r[5] && d.getMinutes() == r[6] && d.getSeconds() == r[7]);
        // }
        // let hh = check(req.body.time)
        // const hhh = new Date(req.query.time)
        // res.send(hhh)
        // return
        const userId = req.params.userId
        const time = new Date(req.query.time) //获取时间
        // console.log(time.getDate());
        const endTime = new Date(time.getTime() + (24 * 60 * 60 * 1000)) //当天的结束时间

        let notices = await Notic.find({
            notifier: userId,
            createdAt:{
                $gte: time,
                $lt: endTime
            }
        }).populate('notifier', ['name']).populate('userId',['name']).sort({_id:-1})
        // res.send(notices)
        if (notices.length === 0) return response(res, 201, '暂无通知~~', null)
        response(res, 200, '获取成功~~', notices)
    } catch (error) {
        next(error)
    }
}

// 根据页数数量获取消息列表
exports.pageNoticeList = async (req,res,next) => {
    try {
        const userId = req.params.userId
        let pagenum = parseInt(req.query.pagenum) || 0 //页数
        let pagesize = parseInt(req.query.pagesize) || 5//条数

        let notices = await Notic.find({
            notifier: userId,
        }).populate('notifier', ['name']).populate('userId', ['name']).sort({_id: -1}).skip(pagenum * pagesize).limit(pagesize)
        .sort({_id:-1})

        let total = await Notic.find({notifier: userId}).count()

        if (notices.length === 0) return response(res, 201, '暂无通知~~', null)
        response(res, 200, '获取成功~~', {
            notices,
            total
        })
    } catch (error) {
        next(error)
    }
}

// 催任务
exports.remindTask = async (req,res,next) => {
    try {
        const taskId = req.params.taskId
        const task = await Task.findById(taskId)

        let notic = new Notic()
        notic.userId = req.body.userId
        notic.taskId = taskId
        notic.notifier = task.worker
        notic.operate = 6
        notic.tips = "催促您赶紧完成 <h3>" + task.taskname + "</h3>"
        notic.createdAt = huanTime(Date.now())
        notic.updatedAt = huanTime(Date.now())
        
        await notic.save()

        response(res, 200, '催促成功~~', notic)

    } catch (error) {
        next(error)
    }
}

// 自定义提示
exports.chat = async (req,res,next) => {
    try {
        const userId = req.params.userId
        const notic = new Notic()
        notic.userId = userId
        notic.notifier = req.body.notifier
        notic.tips = req.body.tips
        notic.operate = 6
        notic.createdAt = huanTime(Date.now())
        notic.updatedAt = huanTime(Date.now())

        await notic.save()

        response(res, 200, '提交成功~~', notic)
    } catch (error) {
        next(error)
    }
}

// 删除通知
exports.deleteNotice = async (req,res,next) => {
    try {
        const noticeId = req.params.noticeId
        let notice = await Notic.findById(noticeId)
        if (!notice) return response(res, 400, '此Id不存在~~', notic)
        let model = await Notic.findByIdAndDelete(noticeId)
        response(res, 200, '删除成功~~', model)
    } catch (error) {
        next(error)
    }
}
